<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + path + "/"; %>

<!DOCTYPE html>
<html>

<head>
    <base href="<%=basePath%>">

    <title>部门物料组配置</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%@ include file="/WEB-INF/jsp/common/header.jsp" %>
<%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

<script type="text/javascript">
    $(function () {
        bindtree();
        bindgrid();
        binduserdept();
    });
</script>
<script>
    function del(node) {
        $.ajax({
            type: "delete",
            url: 'sys/mm/dept/delete?id=' + node.id,
            success: function (data) {
                $.messager.alert('消息', data.msg);
                let dg = $('#dg');
                dg.datagrid('enableFilter');
                dg.datagrid('load');
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.alert('消息', errorThrown);
            }
        });
    }


    function save() {
        if (!$('#fm').form('validate')) {
            return;
        }

        let deptId = $('#userdept').combogrid('getValue');
        if (deptId == '') {
            $.messager.alert('消息', '请选择机构');
            return;
        }
        let model = {
            id: $('#id ').textbox('getValue'),
            groCode: $('#groCode').textbox('getValue'),
            groName: $('#groName').textbox('getValue'),
            corCode: $('#corCode').combobox('getValue'),
            corName: $('#corCode').combobox('getText'),
            facCode: $('#facCode').combobox('getValue'),
            facName: $('#facCode').combobox('getText'),
            deptId: deptId,
            deptCode: $('#deptCode').textbox('getValue'),
            // deptName: $('#deptName').textbox('getValue'),
            delFlag: '0',
            status: '0',
            remark: $('#remark ').textbox('getValue')
        }
        $.ajax({
            type: "post",
            dataType: "json",
            contentType: "application/json",
            url: "sys/mm/dept/save",
            data: JSON.stringify(model),
            success: function (data) {

            },
            complete: function (XMLHttpRequest) {
                if (XMLHttpRequest.responseJSON.code != 60200) {
                    $.messager.alert('消息', XMLHttpRequest.responseJSON.msg);
                } else {
                    $.messager.alert('消息', XMLHttpRequest.responseJSON.msg);
                    let dg = $('#dg');
                    dg.datagrid('enableFilter');
                    dg.datagrid('load');
                    $('#add').window('close');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.alert('消息', errorThrown);
            }
        });
    }

    function editrow() {
        //编辑
        let row = $('#dg').datagrid('getSelected');

        $('#add').window('open');
        if (row) {
            $('#add').window('open');
            $('#fm').form('load', row);
            $('#userdept').combotree('setValue', row.deptId);
        } else {
            $.messager.alert('消息', '请选中一条数据');
        }
    }
</script>
<script>
    function binduserdept() {
        $('#userdept').combotree({
            url: 'DeptManage/DeptListJson_combobox?deptid=' + PUser.deptid
        });
    }

    function bindtree() {
        $('#tt').treegrid({
            title: '组织机构列表',
            url: 'DeptManage/DeptListJson?deptid=' + PUser.deptid,
            iconCls: 'icon-applicationgo',
            nowrap: true,
            width: 300,
            height: 400,
            animate: false,
            collapsible: false,
            singleSelect: true,
            idField: 'deptid',
            treeField: 'dwmc',
            columns: [[{
                field: 'deptid',
                title: '部门id',
                width: 50,
                hidden: true
            }, {
                field: 'dwmc',
                title: '名称',
                width: 240,
                formatter: function (val, rec) {
                    return unescape(val)
                }
            }]],
            onClickRow: function (row) {
                if(!row.dwbm) {
                    $.messager.show({
                        title: '提示',
                        msg: '该部门未配置部门编码',
                        timeout: 3000,
                        showType: 'slide'
                    });
                    return
                }
                bindgrid();
            },
            onLoadSuccess: function () {
                $('#tt').treegrid('collapseAll');
                var rows = $('#tt').treegrid('getChildren');
                if (rows.length) {
                    $('#tt').treegrid('expand', rows[0].dwbm);
                }
            }
        });
    }

    function bindgrid() {
        let deptCode = '';
        let node = $('#tt').treegrid('getSelected');
        if (node != null) {
            deptCode = node.dwbm;
        }
        $('#dg').datagrid({
            title: '物料组列表',
            queryParams: {
                deptCode: deptCode
            },
            url: "sys/mm/dept/selectList",
            iconCls: 'icon-application',
            width: getWidth(0.98),
            height: 415,
            nowrap: true,
            filterDelay: 0,
            rownumbers: true,
            animate: false,
            collapsible: false,
            clientPaging: false,
            remoteFilter: true,
            singleSelect: true,
            pagination: true,
            remoteSort: true,
            frozenColumns: [[]],
            columns: [
                [
                    {
                        field: 'id',
                        title: 'id',
                        sortable: true,
                        hidden: true,
                        formatter: function (val, rec) {
                            return val ? unescape(val) : '';
                        }
                    }, {
                    field: 'groCode',
                    title: '物料组编码',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'groName',
                    title: '物料组名称',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'corCode',
                    title: '公司编码',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'corName',
                    title: '公司名称',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'facCode',
                    title: '工厂编码',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'facName',
                    title: '工厂名称',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'deptId',
                    title: '部门id',
                    sortable: true,
                    hidden: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'deptCode',
                    title: '部门编码',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'deptName',
                    title: '部门名称',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'updateTime',
                    title: '修改时间',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }, {
                    field: 'remark',
                    title: '备注',
                    sortable: true,
                    formatter: function (val, rec) {
                        return val ? unescape(val) : '';
                    }
                }]],
            toolbar: [{
                text: '增加',
                iconCls: 'icon-add',
                handler: function () {
                    $('#dg').datagrid('clearSelections');
                    $('#fm').form('clear');
                    $('#add').window('open');
                    let node = $('#tt').treegrid(
                        'getSelected');
                    if (node != null) {
                        $('#userdept').combotree(
                            'setValue', node.deptid);
                        // 设置单位编码
                        $('#deptCode').textbox('setValue', node.dwbm)
                    }
                    $('#tabs').tabs('select', 0)
                }
            },
                '-',
                {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function () {
                        //编辑
                        var row = $('#dg').datagrid('getSelected');
                        if (row) {
                            editrow(row)
                        } else {
                            $.messager.alert('消息', '请选中一条数据');
                        }
                    }
                },
                '-',
                {
                    text: '删除',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        var row = $('#dg').datagrid('getSelected');
                        if (row) {
                            if (row.status > 0) {
                                $.messager.alert('消息', '已提交，不可删除！');
                                return;
                            }
                            $.messager.confirm('提示', '确定要删除这个吗?', function (r) {
                                if (r) {
                                    del(row);
                                }
                            });
                        } else {
                            $.messager.alert('消息', '请选中一条数据');
                        }
                    }
                }
            ],
            onLoadSuccess: function () {
            },
            onDblClickRow: function () {
                var row = $('#dg').datagrid('getSelected');
                editrow(row);
            }
        });
        //    $('#dg').datagrid('enableFilter');
    }


</script>
<style scoped="scoped">
    .textbox {
        height: 20px;
        margin: 0;
        padding: 0 2px;
        box-sizing: content-box;
    }

    /*.table tr {
height: 15px;
}*/
</style>

<div class="query_criteria" id="query_criteria">
    <table>
        <tr>
            <td>
                <table id="tt" style="height: 500px;"></table>
            </td>
            <td>
                <table id="dg"></table>
            </td>
        </tr>
    </table>
    <div id="add" class="easyui-window" closed="true" modal="true" title="详细信息"
         style="width: 700px; height: 400px; padding: 5px 5px">
        <div id="tabs" class="easyui-tabs" style="width: 650px; height: 350px">
            <div title="基本信息" style="padding: 5px;">
                <form id="fm" class="ssec-form">
                    <table class="table">
                        <tr style="display: none;">
                            <td>
                                <div class="ssec-label">id：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="id"
                                           name="id" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">物料组编码：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="groCode"
                                           name="groCode" class="easyui-textbox"
                                           data-options="required:true"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">物料组名称：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="groName"
                                           name="groName" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">公司编码：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="corCode" name="corCode" style="width: 170px"
                                           class="easyui-combobox" data-options="editable:false,required:false,url:'sysDict/queryItemByTypeName?dicName=公司代码',method:'get',
					                                                          valueField:'dicValue',textField:'dicName',panelHeight:'auto'"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">公司名称：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="corName"
                                           name="corName" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">工厂编码：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="facCode" name="facCode" style="width: 170px"
                                           class="easyui-combobox" data-options="editable:false,required:false,url:'sysDict/queryItemByTypeName?dicName=交货工厂',method:'get',
					                                                          valueField:'dicValue',textField:'dicName',panelHeight:'auto'"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">工厂名称：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="facName"
                                           name="facName" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <div class="ssec-label">所属部门：</div>
                            </td>
                            <td><input class="easyui-combotree" id="userdept" style="width: 230px"/></td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">部门编码：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="deptCode"
                                           name="deptCode" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="ssec-label">备注：</div>
                            </td>
                            <td>
                                <div class="ssec-text normal">
                                    <input id="remark"
                                           name="remark" class="easyui-textbox"
                                           data-options="required:false"/>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <div style="padding: 5px; text-align: center;">
                        <a class="easyui-linkbutton" onclick="save();" iconcls="icon-save">保存</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</div>
</body>

</html>